Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract bindings #128

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

rosek86
Copy link

@rosek86 rosek86 commented Jul 22, 2023

Hello,

I have a use case where size of an application is important, I'd like to webpack as much as possible. The problematic part is related *.node files, I found a potential solution, example with sqlite:

externals: {
    './sqlite3-binding.js':       'commonjs ./node_sqlite3.node',
}

I can replace sqlite3-binding.js [1] with *.node, then I copy node_sqlite3.node to the main application folder. I'd like to try something similar with the serialport. However, it is slightly more complicated since serialport doesn't have single import file. This PR is an attempt to fix the problem.

[1] https://github.com/TryGhost/node-sqlite3/blob/master/lib/sqlite3-binding.js

@td-krzysiek
Copy link

Just a note that for now I solved this with webpack plugin which replaces node-gyp-build with a dummy import file but it would be much more convenient if it is possible to use simple externals as in the example above.

import path from 'node:path';
import { Resolver } from 'webpack';

export const AddonPlugin = {
  apply(resolver: Resolver) {
    resolver.getHook('before-resolve')
      .tapAsync('AddonPlugin', (request, _resolveContext, callback) => {
        if (request.path &&
            request.path?.match(/\@serialport\/bindings-cpp/) &&
            request.request?.match(/node-gyp-build/)) {
          request.request = path.join(__dirname, './serialport.js');
        }
        callback();
      });
  }
};
export default () => require('./addons/serialport.node');

@reconbot
Copy link
Member

This seems fine, however I can't get the actions to run as it's been a long time. Would you be able to do an empty commit and push to your branch to trigger it again? eg git commit --allow-empty -m "Trigger Build"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants